package com.sky.service.impl;

import com.sky.constant.StatusConstant;
import com.sky.entity.Orders;
import com.sky.mapper.DishMapper;
import com.sky.mapper.OrderMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.UserMapper;
import com.sky.service.WorkspaceService;
import com.sky.vo.BusinessDataVO;
import com.sky.vo.DishOverViewVO;
import com.sky.vo.OrderOverViewVO;
import com.sky.vo.SetmealOverViewVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.HashMap;
import java.util.Map;

@Service
@Slf4j
public class WorkspaceServiceImpl implements WorkspaceService {

    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetmealMapper setmealMapper;


    @Override
    public BusinessDataVO todayData(LocalDateTime begin, LocalDateTime end) {
        BusinessDataVO businessDataVO = new BusinessDataVO();
        //新增用户数
        Double sumNewUsers = userMapper.sumNewUsers(begin, end);
        //订单完成率 = 有效订单数/订单总数 * 100%
        Integer status = 5;
        Double sumOrder = orderMapper.sumOrder(begin, end);
        Double vaildOrder = orderMapper.sumTrueOrder(begin, end,status);
        Double orderCompletetionRate = 0.0;
        if(sumOrder != 0 ){
            orderCompletetionRate = vaildOrder/sumOrder;
        }
        //平均客单价 = 营业额/有效订单数
        Map map = new HashMap();
        map.put("begin",begin);
        map.put("end",end);
        map.put("status", Orders.COMPLETED);

        Double turnover = orderMapper.sumByMap(map);
        if(turnover == null){
            turnover = 0.0;
        }
        Double unitPrice = 0.0;
        if(vaildOrder!=null && vaildOrder!=0){
            unitPrice = turnover/vaildOrder;
        }

        //营业额 = 完成订单的总额
        businessDataVO.setNewUsers(sumNewUsers.intValue());
        businessDataVO.setTurnover(turnover);
        businessDataVO.setUnitPrice(unitPrice);
        businessDataVO.setValidOrderCount(vaildOrder.intValue());
        businessDataVO.setOrderCompletionRate(orderCompletetionRate);



        return businessDataVO;
    }
}
